package com.l2jserver.gameserver.communitybbs.Manager.Casino.SlotMachine;

import java.text.DecimalFormat;
import java.util.StringTokenizer;

import com.l2jserver.Config;
import com.l2jserver.gameserver.cache.HtmCache;
import com.l2jserver.gameserver.communitybbs.Model.BaseBBSManager;
import com.l2jserver.gameserver.datatables.ImagesTables;
import com.l2jserver.gameserver.datatables.ItemTable;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
import com.l2jserver.gameserver.model.items.L2Item;
import com.l2jserver.gameserver.network.clientpackets.Say2;
import com.l2jserver.gameserver.network.serverpackets.CreatureSay;
import com.l2jserver.gameserver.network.serverpackets.MagicSkillUse;
import com.l2jserver.gameserver.network.serverpackets.ShowBoard;
import com.l2jserver.util.Rnd;

/**
 * Las chances para realizar los diferentes pagos son efectuados a mi parecer.<br>
 * Segun mis formulas el servidor solo quedara con un 3% o menos de lo jugado.<br>
 * @author fissban
 */
public class SlotMachineBBSManager extends BaseBBSManager
{
	// @formatter:off
	private final int[] RODILLO =
	{
		9004, 9004, 9004
	};

	private final int[] LIST_PAGOS =
	{
		2000, 100, 50, 30, 15, 10, 5, 3, 2, 0
	};
	// @formatter:on
	
	private int PAGO = 0;
	private int MULTIPLICADOR = 0;
	private int CONTADOR = 1;
	
	@Override
	public void parseCmd(String command, L2PcInstance activeChar)
	{
		// enviamos las imagenes necesarias
		for (int imgId : ImagesTables.getInstance()._img_slot_id)
		{
			ImagesTables.getInstance().sendImage(activeChar, imgId);
		}
		
		if (command.equals("_bbsslot"))
		{
			String content = HtmCache.getInstance().getHtm(activeChar.getHtmlPrefix(), "data/html/CommunityBoard/custom/casino/SlotMachine/index.htm");
			
			final String firstPartImg = "<img src=Crest.crest_" + Config.SERVER_ID + "_";
			
			content = content.replaceAll("%player%", activeChar.getName());
			
			content = content.replaceAll("%img1%", String.valueOf(firstPartImg + RODILLO[0]));
			content = content.replaceAll("%img2%", String.valueOf(firstPartImg + RODILLO[1]));
			content = content.replaceAll("%img3%", String.valueOf(firstPartImg + RODILLO[2]));
			
			final L2Item item = ItemTable.getInstance().getTemplate(Config.ITEM_ID_CASINO_SLOT);
			content = content.replaceAll("%icon%", String.valueOf(item.getIcon()));
			
			DecimalFormat df = new DecimalFormat("###,###,###,###");
			content = content.replaceAll("%credits%", String.valueOf(df.format(activeChar.getInventory().getInventoryItemCount(Config.ITEM_ID_CASINO_SLOT, -1))));
			content = content.replaceAll("%payment%", String.valueOf(df.format(PAGO * MULTIPLICADOR) + " " + item.getName()));
			
			content = content.replaceAll("%valor%", String.valueOf(Config.BET_SLOT));
			
			content = content.replaceAll("%name%", String.valueOf(item.getName()));
			
			separateAndSend(content, activeChar);
		}
		else if (command.startsWith("_bbsslot"))
		{
			final StringTokenizer st = new StringTokenizer(command, ";");
			st.nextToken();// command
			
			switch (st.nextToken())
			{
				case "jugada":
					MULTIPLICADOR = Config.BET_SLOT;
					
					if (activeChar.getInventory().getInventoryItemCount(Config.ITEM_ID_CASINO_SLOT, -1) >= Config.BET_SLOT)
					{
						nextGame(); // generamos el pago y los rodillos
						activeChar.destroyItemByItemId("casino jugada", Config.ITEM_ID_CASINO_SLOT, Config.BET_SLOT, activeChar, false);
						
						if (PAGO > 0)
						{
							if (PAGO >= 50)
							{
								activeChar.broadcastPacket(new MagicSkillUse(activeChar, activeChar, 2025, 1, 1, 0));// fuegos artifciales
							}
							activeChar.addItem("casino pago", Config.ITEM_ID_CASINO_SLOT, PAGO * Config.BET_SLOT, activeChar, false);
						}
					}
					else
					{
						activeChar.sendPacket(new CreatureSay(0, Say2.TELL, "[SlotMachine]", "You do not have enough adena to play"));
					}
					
					parseCmd("_bbsslot", activeChar);// hacemos q se actualize el html
					break;
			}
		}
		else
		{
			ShowBoard sb = new ShowBoard("<html><body><br><br><center>casino is not implemented yet</center><br><br></body></html>", "101");
			activeChar.sendPacket(sb);
			activeChar.sendPacket(new ShowBoard(null, "102"));
			activeChar.sendPacket(new ShowBoard(null, "103"));
		}
	}
	
	private void nextGame()
	{
		// contador de vueltas
		// dependiendo de la cant del pago anterior sera la cant de vueltas q pasaras sin volver a ganar
		// damos una chance de 20% para q no sea tan obvio el sistema, igual el jugador se le seguira acumulando
		// la cant de veces sin cobrar
		if ((CONTADOR != 0) && (Rnd.get(CONTADOR) != 0))
		{
			// generamos combinacion perdedora
			RODILLO[0] = ImagesTables.getInstance()._img_slot_id[Rnd.get(2, 9)];
			RODILLO[1] = ImagesTables.getInstance()._img_slot_id[Rnd.get(2, 9)];
			RODILLO[2] = ImagesTables.getInstance()._img_slot_id[Rnd.get(2, 9)];
			PAGO = LIST_PAGOS[9];
			CONTADOR--;
		}
		else
		{
			final int chance = Rnd.get(2001);
			
			if (chance >= 1980) // 0.05%
			{
				// $$$
				RODILLO[0] = ImagesTables.getInstance()._img_slot_id[5];
				RODILLO[1] = ImagesTables.getInstance()._img_slot_id[5];
				RODILLO[2] = ImagesTables.getInstance()._img_slot_id[5];
				PAGO = LIST_PAGOS[0];
			}
			else if (chance >= 1978)// 0.95%
			{
				// 777
				RODILLO[0] = ImagesTables.getInstance()._img_slot_id[4];
				RODILLO[1] = ImagesTables.getInstance()._img_slot_id[4];
				RODILLO[2] = ImagesTables.getInstance()._img_slot_id[4];
				PAGO = LIST_PAGOS[1];
			}
			else if (chance >= 1940) // 1.80%
			{
				// bar3 bar3 bar3
				RODILLO[0] = ImagesTables.getInstance()._img_slot_id[3];
				RODILLO[1] = ImagesTables.getInstance()._img_slot_id[3];
				RODILLO[2] = ImagesTables.getInstance()._img_slot_id[3];
				PAGO = LIST_PAGOS[2];
			}
			else if (chance >= 1853) // 2.75%
			{
				// bar2 bar2 bar2
				RODILLO[0] = ImagesTables.getInstance()._img_slot_id[2];
				RODILLO[1] = ImagesTables.getInstance()._img_slot_id[2];
				RODILLO[2] = ImagesTables.getInstance()._img_slot_id[2];
				PAGO = LIST_PAGOS[3];
			}
			else if (chance >= 1748) // 4.55%
			{
				// bar1 bar1 bar1
				RODILLO[0] = ImagesTables.getInstance()._img_slot_id[1];
				RODILLO[1] = ImagesTables.getInstance()._img_slot_id[1];
				RODILLO[2] = ImagesTables.getInstance()._img_slot_id[1];
				PAGO = LIST_PAGOS[4];
			}
			else if (chance >= 1626) // 8.3%
			{
				// cher cher cher
				RODILLO[0] = ImagesTables.getInstance()._img_slot_id[0];
				RODILLO[1] = ImagesTables.getInstance()._img_slot_id[0];
				RODILLO[2] = ImagesTables.getInstance()._img_slot_id[0];
				PAGO = LIST_PAGOS[5];
			}
			else if (chance >= 1286) // 12% + 100
			{
				// 1 o 2 bar1
				// aca ya no hace falta q sean los 3 iguales
				if (Rnd.get(100) > 9) // generamos la chance de q salgan 2 simbolos iguales
				{
					switch (Rnd.get(3))
					{
						case 2:
							RODILLO[0] = ImagesTables.getInstance()._img_slot_id[Rnd.get(6, 9)];
							RODILLO[1] = ImagesTables.getInstance()._img_slot_id[1];
							RODILLO[2] = ImagesTables.getInstance()._img_slot_id[1];
							break;
						case 1:
							RODILLO[0] = ImagesTables.getInstance()._img_slot_id[1];
							RODILLO[1] = ImagesTables.getInstance()._img_slot_id[1];
							RODILLO[2] = ImagesTables.getInstance()._img_slot_id[Rnd.get(6, 9)];
							break;
						case 0:
							RODILLO[0] = ImagesTables.getInstance()._img_slot_id[1];
							RODILLO[1] = ImagesTables.getInstance()._img_slot_id[Rnd.get(6, 9)];
							RODILLO[2] = ImagesTables.getInstance()._img_slot_id[1];
							break;
					}
				}
				else
				{
					switch (Rnd.get(3))
					{
						case 2:
							RODILLO[0] = ImagesTables.getInstance()._img_slot_id[Rnd.get(6, 9)];
							RODILLO[1] = ImagesTables.getInstance()._img_slot_id[Rnd.get(6, 9)];
							RODILLO[2] = ImagesTables.getInstance()._img_slot_id[1];
							break;
						case 1:
							RODILLO[0] = ImagesTables.getInstance()._img_slot_id[Rnd.get(6, 9)];
							RODILLO[1] = ImagesTables.getInstance()._img_slot_id[1];
							RODILLO[2] = ImagesTables.getInstance()._img_slot_id[Rnd.get(6, 9)];
							break;
						case 0:
							RODILLO[0] = ImagesTables.getInstance()._img_slot_id[1];
							RODILLO[1] = ImagesTables.getInstance()._img_slot_id[Rnd.get(6, 9)];
							RODILLO[2] = ImagesTables.getInstance()._img_slot_id[Rnd.get(6, 9)];
							break;
					}
				}
				PAGO = LIST_PAGOS[6];
			}
			else if (chance >= 926) // 18%
			{
				// 2 cheer juntas
				switch (Rnd.get(3))
				{
					case 2:
						RODILLO[0] = ImagesTables.getInstance()._img_slot_id[1];
						RODILLO[1] = ImagesTables.getInstance()._img_slot_id[1];
						RODILLO[2] = ImagesTables.getInstance()._img_slot_id[Rnd.get(2, 9)];
						break;
					case 1:
						RODILLO[0] = ImagesTables.getInstance()._img_slot_id[Rnd.get(2, 9)];
						RODILLO[1] = ImagesTables.getInstance()._img_slot_id[1];
						RODILLO[2] = ImagesTables.getInstance()._img_slot_id[1];
						break;
					case 0:
						RODILLO[0] = ImagesTables.getInstance()._img_slot_id[1];
						RODILLO[1] = ImagesTables.getInstance()._img_slot_id[Rnd.get(2, 9)];
						RODILLO[2] = ImagesTables.getInstance()._img_slot_id[1];
						break;
				}
				
				PAGO = LIST_PAGOS[7];
			}
			else if (chance >= 626) // 20%
			{
				// 1 sola cheer
				switch (Rnd.get(3))
				{
					case 2:
						RODILLO[0] = ImagesTables.getInstance()._img_slot_id[0];
						RODILLO[1] = ImagesTables.getInstance()._img_slot_id[Rnd.get(1, 9)];
						RODILLO[2] = ImagesTables.getInstance()._img_slot_id[Rnd.get(1, 9)];
						break;
					case 1:
						RODILLO[0] = ImagesTables.getInstance()._img_slot_id[Rnd.get(1, 9)];
						RODILLO[1] = ImagesTables.getInstance()._img_slot_id[0];
						RODILLO[2] = ImagesTables.getInstance()._img_slot_id[Rnd.get(1, 9)];
						break;
					case 0:
						RODILLO[0] = ImagesTables.getInstance()._img_slot_id[Rnd.get(1, 9)];
						RODILLO[1] = ImagesTables.getInstance()._img_slot_id[Rnd.get(1, 9)];
						RODILLO[2] = ImagesTables.getInstance()._img_slot_id[0];
						break;
				}
				PAGO = LIST_PAGOS[8];
			}
			else
			{
				RODILLO[0] = ImagesTables.getInstance()._img_slot_id[Rnd.get(2, 9)];
				RODILLO[1] = ImagesTables.getInstance()._img_slot_id[Rnd.get(2, 9)];
				RODILLO[2] = ImagesTables.getInstance()._img_slot_id[Rnd.get(2, 9)];
				PAGO = LIST_PAGOS[9];
			}
			CONTADOR += PAGO;
		}
	}
	
	@Override
	public void parseWrite(String ar1, String ar2, String ar3, String ar4, String ar5, L2PcInstance activeChar)
	{
		//
	}
	
	public SlotMachineBBSManager()
	{
		//
	}
	
	public static SlotMachineBBSManager getInstance()
	{
		return SingletonHolder._instance;
	}
	
	private static class SingletonHolder
	{
		protected static final SlotMachineBBSManager _instance = new SlotMachineBBSManager();
	}
}